package com.cerosoft.api.http.webservices;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.cerosoft.api.exceptions.ConnectionException;
import com.cerosoft.api.http.ProxyFactory;
import com.cerosoft.api.http.iface.IProgressCallback;
import com.cerosoft.api.http.models.Asset;
import com.cerosoft.api.http.models.HttpResult;
import com.cerosoft.api.http.models.MediaInfo;
import com.cerosoft.utils.MediaXMLHelper;

public class MediaBrowserWebServiceProxy {

	public static final String SAMPLE_THUMB_JPG = "sample_thumb.jpg";

	private static final String root = "/Root";

	private static final String content = "/Content";

	private static final String thumbnail = "/Thumbnail";

	private static final String download = "/Download";

	private static MediaBrowserWebServiceProxy instance = new MediaBrowserWebServiceProxy();

	private Map<String, String> parameters = new HashMap<String, String>();

	private MediaBrowserWebServiceProxy() {
	}

	public static MediaBrowserWebServiceProxy getInstance() {
		return instance;
	}

	/**
	 * 
	 * @param ipAddr
	 * @return
	 * @throws ConnectionException
	 */
	public List<MediaInfo> getFolderList(final String ipAddr)
			throws ConnectionException {

		String url = ipAddr + root;

		HttpResult<String> result = ProxyFactory.getDefaultProxy().get(url,
				null, true);

		List<MediaInfo> ucUnserialize = MediaXMLHelper.getFolderList(result
				.getValue());

		return ucUnserialize;

	}

	public String getStringValue(final String url) throws ConnectionException {

		HttpResult<String> result = ProxyFactory.getDefaultProxy().get(url,
				null, false);

		return result.getValue();

	}

	public List<MediaInfo> getFolderInfo(final String ipAddr, final String path)
			throws ConnectionException {

		parameters.clear();
		parameters.put("path", path);

		HttpResult<String> result = ProxyFactory.getDefaultProxy().get(
				ipAddr + content, parameters, true);

		List<MediaInfo> ucUnserialize = MediaXMLHelper.getFolderInfo(
				result.getValue(), path);

		return ucUnserialize;

	}

	public Bitmap getThumbnail(Context context, final String ipAddr,
			final String path) throws ConnectionException {

		parameters.clear();
		parameters.put("path", path);

		HttpResult<String> result = ProxyFactory.getDefaultProxy().get(
				ipAddr + thumbnail, parameters, true);
		// byte[] datas = result.getValue().getBytes();

		@SuppressWarnings("deprecation")
		Bitmap bitmap = BitmapFactory.decodeStream(new StringBufferInputStream(
				result.getValue()));

		return bitmap;

	}

	private Bitmap getBitmap(final String url) {
		URL myUrl = null;
		Bitmap bitmap = null;
		try {
			myUrl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		InputStream is = null;
		HttpURLConnection conn = null;
		if (myUrl != null) {
			try {
				conn = (HttpURLConnection) myUrl.openConnection();
				conn.setDoInput(true);
				conn.connect();
				is = conn.getInputStream();
				bitmap = BitmapFactory.decodeStream(is);
				is.close();
				conn.disconnect();
			} catch (IOException e) {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
				if (conn != null) {
					conn.disconnect();
				}
			}
		}
		return bitmap;
	}

	public Bitmap getThumbnailViaURL(final String ipAddr, final String path) {
		Log.d("m", "getThumbnailViaURL path=" + path);
		String url = ipAddr + thumbnail + "?path=" + URLEncoder.encode(path);
		Log.d("m", "getThumbnailViaURL url=" + url);

		return getBitmap(url);

	}

	public Bitmap getBitmapViaURL(final String ipAddr, final String path) {
		Log.d("m", "getBitmapViaURL path=" + path);
		String url = ipAddr + download + "?path=" + URLEncoder.encode(path);
		Log.d("m", "getThumbnailViaURL url=" + url);

		return getBitmap(url);
	}

	public void downloadFile(final String ipAddr, final String path,
			IProgressCallback callback) throws ConnectionException {

		parameters.clear();
		parameters.put("path", path);

		HttpResult<Asset> result = ProxyFactory.getDefaultProxy().download(
				ipAddr + content, parameters, callback);

	}

}
